<?php
namespace Action;
use HY\Action;
!defined('HY_PATH') && exit('HY_PATH not defined.');
class Index extends HYBBS {
	public function __construct(){
		parent::__construct();
		//{hook a_index_init}
		if(!IS_LOGIN){
			if(IS_AJAX && IS_POST){
				die($this->json(array('error'=>false,'info'=>'请登录后再操作')));
			}
			else{
				header("location: ". HYBBS_URLA('user','login'));
				die;
			}

		}
	}
	public function Index(){

		//$rueList=M("Sysinfo")->request_get("http://127.0.0.1:8080/hi?command=list-rule");
		//$this->json($rueList);
		$this->view = 'CQQ';
		$this->v("title","流量控制");
        $this->v("title2","流量控制系统");
		$this->display('index');
	}
	//显示终端列表
	public function elem(){

	
		$this->view = 'CQQ';
		$this->v("title","流量控制");
        $this->v("title2","流量控制系统");
		$this->display('elem');
	}
	public function delRule(){
		$rule_id = X('post.rule_id');
		$result=M("Sysinfo")->request_get("http://127.0.0.1:8080/hi?command=del-rule&rule_id=".$rule_id);
		$result = (array)json_decode($result);
		$this->json([
			'state'=>true,
			"res"=>$result,
			"info"=>"已删除"
		]);
	}
	public function addRule(){
		$rule_id=X('post.rule_id',0);
		$address_type = X('post.address_type');
        $address_first=X('post.address_first');
        $address_second=X('post.address_second');
        $port_type=X('post.port_type');
        $port_first=X('post.port_first');
        $port_second=X('post.port_second');
        $protocol_type=X('post.protocol_type');
        $rate_value=X('post.rate_value');
		//如果制定ruleid为0 表示添加 否则 就是修改 需要先删除
		if($rule_id>0){
			$result=M("Sysinfo")->request_get("http://127.0.0.1:8080/hi?command=del-rule&rule_id=".$rule_id);
		}
		$rueList=M("Sysinfo")->request_get("http://127.0.0.1:8080/hi?command=add-rule&address_type=".$address_type.
			"&address_first=".$address_first."&address_second=".$address_second."&port_type=".$port_type.
			"&port_first=".$port_first.
			"&port_second=".$port_second."&protocol_type=".$protocol_type."&rate_value=".$rate_value);
			//$rueList=M("Sysinfo")->request_get("http://127.0.0.1:8080/hi?command=list-rule");
		$this->json([
			'state'=>true,
			"info"=>"已添加",
			"return"=>$rueList
		]);

	}
	public function getElem(){
		$elem_id = X('post.elem_id',0);
		//不指定ruleid 就返回全部
		if($elem_id == 0){
			$result=M("Sysinfo")->request_get("http://127.0.0.1:8080/hi?command=list-elem");
			//$this->json($result);
			$result = (array)json_decode($result);
			//$this->json($result['data']);
			$count = count($result['data']);
			$resultList=[];
			foreach ($result['data'] as $key => $value) {
				$Info = M("Sysinfo")->request_get("http://127.0.0.1:8080/hi?command=get-elem&address=".$value);
				$Info = (array)json_decode($Info);
				//$this->json([$ruleInfo,$value]);
				array_push($resultList,$Info['data']);
				
			}		
			$this->json([
				'state'=>true,
				"rules"=>$resultList,
				"count"=>$count
			]);
		}
		
	}
	public function getRule(){
		$rule_id = X('post.rule_id',0);
		//不指定ruleid 就返回全部
		if($rule_id == 0){
			$result=M("Sysinfo")->request_get("http://127.0.0.1:8080/hi?command=list-rule");
			$result = (array)json_decode($result);
			//$this->json($result['data']);
			$countRule = count($result['data']);
			$resultList=[];
			foreach ($result['data'] as $key => $value) {
				$ruleInfo = M("Sysinfo")->request_get("http://127.0.0.1:8080/hi?command=get-rule&rule_id=".$value);
				$ruleInfo = (array)json_decode($ruleInfo);
				//$this->json([$ruleInfo,$value]);
				array_push($resultList,$ruleInfo['data']);
				
			}		
			$this->json([
				'state'=>true,
				"rules"=>$resultList,
				"count"=>$countRule
			]);
		}else{
			//制定ruleid 就返回该规则信息
			$ruleInfo = M("Sysinfo")->request_get("http://127.0.0.1:8080/hi?command=get-rule&rule_id=".$rule_id);
			$ruleInfo = (array)json_decode($ruleInfo);
			$this->json([
				'state'=>true,
				"rule"=>$ruleInfo["data"],
			]);

		}
		
	}
	
	public function getSysInfo(){
		$this->json([
			"state"=>true,
			"info"=>M("Sysinfo")->getSysInfo()
		]);

    }
	
	//{hook a_index_fun}
}
